from utility.sql_helper import MysqlHelper


class Users(object):
    def __init__(self):
        self._sql = MysqlHelper()

    def login(self, username):
        sql = 'SELECT * FROM useraccount WHERE username=%s;'
        params = username
        res = self._sql.get_all(sql, params)
        return res

    def update_last_visit_time(self, username):
        sql = 'UPDATE useraccount SET last_visit_time=(SELECT TIMESTAMP(NOW()))  WHERE username=%s;'
        params = username
        res = self._sql.update(sql, params)
        return res

    def register(self, username, password, nickname, email, phone):
        sql = 'INSERT INTO useraccount(username, password, nickname, email, phone) VALUES (%s, %s, %s, %s, %s);'
        params = (username, password, nickname, email, phone)
        res = self._sql.insert(sql, params)
        return res

    def update_userinfo(self, nickname, phone, email, user_id):
        sql = 'UPDATE useraccount SET nickname=%s, phone=%s, email=%s WHERE user_id=%s;'
        params = (nickname, phone, email, user_id)
        res = self._sql.update(sql, params)
        return res

    def get_password(self, user_id):
        sql = 'SELECT * FROM useraccount WHERE user_id=%s;'
        params = user_id
        res = self._sql.get_all(sql, params)
        return res

    def update_password(self, password, user_id):
        sql = 'UPDATE useraccount SET password=%s WHERE user_id=%s;'
        params = (password, user_id)
        res = self._sql.update(sql, params)
        return res

    def delete_user(self, user_id):
        sql = 'DELETE FROM useraccount WHERE user_id=%s;'
        params = user_id
        res = self._sql.delete(sql, params)
        return res

    # def update_locked(self, user_id):
    #     sql = 'UPDATE useraccount SET locked=~locked WHERE user_id=%s;'
    #     params = user_id
    #     res = self._helper.update(sql, params)
    #     return res
    #
